package com.fw.ui.view.common;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

import com.fw.ui.bao.MessageBAO;
import com.fw.ui.util.UICommonUtils;
import com.fw.ui.util.VaadinApplicationObjectSupport;
import com.fw.ui.view.AbstractPanel;
import com.fw.ui.view.common.event.LoginPanelClearButtonClickListener;
import com.fw.ui.view.common.event.LoginPanelLoginButtonClickListener;
import com.vaadin.event.ShortcutAction.KeyCode;
import com.vaadin.event.ShortcutListener;
import com.vaadin.server.ThemeResource;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Image;
import com.vaadin.ui.Label;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

@org.springframework.stereotype.Component
@Scope(SCOPE_PROTOTYPE)
public class LoginPanel extends AbstractPanel {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public static final String LOGIN_TYPE_BO = "LOGIN_TYPE_BO";
    public static final String LOGIN_TYPE_MC = "LOGIN_TYPE_MC";

    @Autowired
    protected transient VaadinApplicationObjectSupport app;
    @Autowired
    private LoginPanelLoginButtonClickListener loginPanelLoginButtonClickListener;
    @Autowired
    private LoginPanelClearButtonClickListener loginPanelClearButtonClickListener;
    @Autowired
    private MessageBAO messageBAO;

    @Autowired
    private CaptchaPanel captchaPanel;

    private Label lblTitle = new Label("", ContentMode.HTML);
    private Label lblUsername = new Label("", ContentMode.HTML);
    private Label lblPassword = new Label("", ContentMode.HTML);
    private Label lblUsernameValidateMessage = new Label("", ContentMode.HTML);
    private Label lblPasswordValidateMessage = new Label("", ContentMode.HTML);

    private Label lblFooter = new Label("", ContentMode.HTML);

    private TextField txtUsername = new TextField();
    private PasswordField txtPassword = new PasswordField();

    private Button btnLogin = new Button();
    private Button btnClear = new Button();

    private HorizontalLayout row1Validate = new HorizontalLayout();
    private HorizontalLayout row2Validate = new HorizontalLayout();

    @Override
    public void updateStrings() {
	lblTitle.setValue(this.messageBAO.getMessage("com.fw.ui.view.common.LoginPanel.label.title"));
	lblFooter.setValue(this.messageBAO.getMessage("application.title.footer"));
	lblUsername.setValue(this.messageBAO.getMessage("com.fw.ui.view.common.LoginPanel.label.username"));
	lblPassword.setValue(this.messageBAO.getMessage("com.fw.ui.view.common.LoginPanel.label.password"));
	btnLogin.setCaption(this.messageBAO.getMessage("com.fw.ui.view.common.LoginPanel.button.login"));
	btnClear.setCaption(this.messageBAO.getMessage("com.fw.ui.view.common.LoginPanel.button.clear"));

	txtUsername.setInputPrompt(this.messageBAO.getMessage("com.fw.ui.view.common.LoginPanel.inputprompt.username"));
	txtPassword.setInputPrompt(this.messageBAO.getMessage("com.fw.ui.view.common.LoginPanel.inputprompt.password"));
    }

    @Override
    public void init() {
	this.setSizeFull();
	this.setId("loginPanel");

	ThemeResource iconThemeResource = new ThemeResource("img/AEONLogo.png");
	Image iconPic = new Image(null, iconThemeResource);
	iconPic.addStyleName("header-bandner-icon");

	this.lblUsernameValidateMessage.setId("lblUsernameValidateMessage" + UICommonUtils.INCLUDE_VALUE_COMPONENT_PREFIX);
	this.lblPasswordValidateMessage.setId("lblPasswordValidateMessage" + UICommonUtils.INCLUDE_VALUE_COMPONENT_PREFIX);

	this.row1Validate.setId("row1Validate" + UICommonUtils.INCLUDE_VISIBLE_COMPONENT_PREFIX);
	this.row2Validate.setId("row2Validate" + UICommonUtils.INCLUDE_VISIBLE_COMPONENT_PREFIX);

	lblTitle.addStyleName("h2");
	lblFooter.setSizeUndefined();
	lblFooter.addStyleName("footer-layout-label");

	lblUsername.setWidth("100px");
	lblPassword.setWidth("100px");

	Label lblSep1 = new Label();
	Label lblSep2 = new Label();
	lblSep1.setWidth("100px");
	lblSep2.setWidth("100px");

	this.lblUsernameValidateMessage.addStyleName("validate-error");
	this.lblPasswordValidateMessage.addStyleName("validate-error");

	txtUsername.focus();
	txtUsername.setWidth("200px");
	txtPassword.setWidth("200px");
	txtUsername.setMaxLength(50);
	txtPassword.setMaxLength(200);

	this.lblUsernameValidateMessage.setWidth("200px");
	this.lblPasswordValidateMessage.setWidth("200px");

	btnLogin.addStyleName("button-blue");
	btnClear.addStyleName("button-red");
	btnLogin.setWidth("70px");
	btnClear.setWidth("70px");
	btnLogin.setDisableOnClick(true);
	btnClear.setDisableOnClick(true);

	VerticalLayout rootlayout = new VerticalLayout();
	rootlayout.setSizeFull();

	VerticalLayout mainlayout = new VerticalLayout();
	mainlayout.setSpacing(true);
	mainlayout.setSizeUndefined();

	VerticalLayout loginLayout = new VerticalLayout();
	loginLayout.setSpacing(true);
	loginLayout.setMargin(true);
	loginLayout.setSizeUndefined();
	loginLayout.addStyleName("login-panel");

	HorizontalLayout headerlayout = new HorizontalLayout();
	headerlayout.setSpacing(true);
	headerlayout.setSizeUndefined();

	VerticalLayout formLayout = new VerticalLayout();
	formLayout.setId("formPanel");
	formLayout.setSizeFull();
	formLayout.setSpacing(true);

	HorizontalLayout row1 = new HorizontalLayout();
	HorizontalLayout row2 = new HorizontalLayout();
	HorizontalLayout row3 = new HorizontalLayout();

	row1.setSpacing(true);
	row1Validate.setSpacing(true);
	row2.setSpacing(true);
	row2Validate.setSpacing(true);
	row3.setSpacing(true);

	row1Validate.setVisible(false);
	row2Validate.setVisible(false);
	captchaPanel.setVisible(false);

	headerlayout.addComponent(iconPic);
	headerlayout.addComponent(lblTitle);
	headerlayout.setComponentAlignment(iconPic, Alignment.MIDDLE_CENTER);
	headerlayout.setComponentAlignment(lblTitle, Alignment.BOTTOM_CENTER);

	row1.addComponent(lblUsername);
	row1.addComponent(txtUsername);
	row1.setComponentAlignment(lblUsername, Alignment.MIDDLE_CENTER);
	row1.setComponentAlignment(txtUsername, Alignment.MIDDLE_CENTER);

	row2.addComponent(lblPassword);
	row2.addComponent(txtPassword);
	row2.setComponentAlignment(lblPassword, Alignment.MIDDLE_CENTER);
	row2.setComponentAlignment(txtPassword, Alignment.MIDDLE_CENTER);

	row3.addComponent(btnLogin);
	row3.addComponent(btnClear);

	row1Validate.addComponent(lblSep1);
	row1Validate.addComponent(lblUsernameValidateMessage);
	row1Validate.setComponentAlignment(lblUsernameValidateMessage, Alignment.TOP_LEFT);
	row2Validate.addComponent(lblSep2);
	row2Validate.addComponent(lblPasswordValidateMessage);
	row2Validate.setComponentAlignment(lblPasswordValidateMessage, Alignment.TOP_LEFT);

	formLayout.addComponent(row1);
	formLayout.addComponent(row1Validate);
	formLayout.addComponent(row2);
	formLayout.addComponent(row2Validate);
	formLayout.addComponent(captchaPanel);
	formLayout.addComponent(row3);

	formLayout.setComponentAlignment(row1, Alignment.MIDDLE_CENTER);
	formLayout.setComponentAlignment(row1Validate, Alignment.TOP_CENTER);
	formLayout.setComponentAlignment(row2, Alignment.MIDDLE_CENTER);
	formLayout.setComponentAlignment(row2Validate, Alignment.TOP_CENTER);
	formLayout.setComponentAlignment(captchaPanel, Alignment.MIDDLE_CENTER);
	formLayout.setComponentAlignment(row3, Alignment.MIDDLE_CENTER);

	loginLayout.addComponent(headerlayout);
	loginLayout.addComponent(formLayout);
	loginLayout.setComponentAlignment(formLayout, Alignment.MIDDLE_CENTER);

	mainlayout.addComponent(loginLayout);
	mainlayout.addComponent(new HorizontalLayout());
	mainlayout.addComponent(new HorizontalLayout());
	mainlayout.addComponent(lblFooter);
	mainlayout.setComponentAlignment(loginLayout, Alignment.MIDDLE_CENTER);
	mainlayout.setComponentAlignment(lblFooter, Alignment.MIDDLE_CENTER);

	rootlayout.addComponent(mainlayout);
	rootlayout.setComponentAlignment(mainlayout, Alignment.MIDDLE_CENTER);
	this.setContent(rootlayout);
    }

    @Override
    public void initListener() {
	final ShortcutListener enter = new ShortcutListener("Login", KeyCode.ENTER, null) {

	    private static final long serialVersionUID = 1L;

	    @Override
	    public void handleAction(Object sender, Object target) {
		btnLogin.click();
	    }
	};

	this.loginPanelLoginButtonClickListener.setBasedComponent(this);
	this.loginPanelClearButtonClickListener.setBasedComponent(this);

	this.loginPanelLoginButtonClickListener.setAutoEnabled(true);
	this.loginPanelClearButtonClickListener.setAutoEnabled(true);

	this.loginPanelLoginButtonClickListener.setShortcutListener(enter);

	this.btnLogin.addShortcutListener(enter);
	this.btnLogin.addClickListener(this.loginPanelLoginButtonClickListener);
	this.btnClear.addClickListener(this.loginPanelClearButtonClickListener);
    }

    @Override
    public void initValidator() {

    }

    public void initData() {

    }

    public MessageBAO getMessageBAO() {
	return messageBAO;
    }

    public Label getLblTitle() {
	return lblTitle;
    }

    public Label getLblUsername() {
	return lblUsername;
    }

    public Label getLblPassword() {
	return lblPassword;
    }

    public Label getLblUsernameValidateMessage() {
	return lblUsernameValidateMessage;
    }

    public Label getLblPasswordValidateMessage() {
	return lblPasswordValidateMessage;
    }

    public Label getLblFooter() {
	return lblFooter;
    }

    public TextField getTxtUsername() {
	return txtUsername;
    }

    public PasswordField getTxtPassword() {
	return txtPassword;
    }

    public Button getBtnLogin() {
	return btnLogin;
    }

    public Button getBtnClear() {
	return btnClear;
    }

    public HorizontalLayout getRow1Validate() {
	return row1Validate;
    }

    public HorizontalLayout getRow2Validate() {
	return row2Validate;
    }

    public CaptchaPanel getCaptchaPanel() {
	return captchaPanel;
    }

}
